package com.chain.BrotherExchange.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.ui.adapter.LegalTenderTransactionListAdapter;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateFragment;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.viewmodel.LegalTenderDealViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;

/**
 * 用户法币交易列表
 */
public class UserLegalTenderDealListFragment extends AbsNetworkStateFragment implements OnRefreshLoadmoreListener {

    @Bind(R.id.recycler_view) RecyclerView mRecyclerView;
    @Bind(R.id.smart_refresh_layout) SmartRefreshLayout mSmartRefreshLayout;

    private int mUid;
    private int mPageType;
    private LegalTenderDealViewModel mViewModel;
    private LegalTenderTransactionListAdapter mAdapter;

    @Override
    protected int getLayoutID() {
        return R.layout.simple_load_refresh_recycler_view;
    }

    @Override
    protected void initActions() {
        Bundle arguments = getArguments();
        mPageType = arguments.getInt(C.EXTRA_PAGE_TYPE);
        mUid = arguments.getInt(C.EXTRA_UID);

        // 初始化列表
        mAdapter = new LegalTenderTransactionListAdapter(mUid);
        mRecyclerView.setAdapter(mAdapter);
        mSmartRefreshLayout.setOnRefreshLoadmoreListener(this);

        // 数据
        mViewModel = ViewModelProviders.of(this).get(LegalTenderDealViewModel.class);
        initLegalTenderDealList();
    }

    private void initLegalTenderDealList() {
        mViewModel.newLegalTenderUserDealList(mPageType, mUid).observe(this, resource -> {
                    if (mSmartRefreshLayout.isRefreshing()) {
                        mSmartRefreshLayout.finishRefresh();
                    }

                    Resource.load(getContext(), resource, this, result ->
                            mAdapter.setLegalTenderDealList(result)
                    );
                }
        );
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mViewModel.moreLegalTenderUserDealList(mPageType, mUid).observe(this, resource -> {
            switch (resource.state) {
                case EMPTY:
                    Androids.makeText(R.string.no_more);
                    break;
                case NOT_NETWORK:
                    Androids.makeText(R.string.network_error);
                    break;
                case ERROR:
                    Androids.makeText(resource.message);
                    break;
                case SUCCESS:
                    mAdapter.addLegalTenderDealList(resource.source);
                    break;
            }

            mSmartRefreshLayout.finishLoadmore(0);
        });
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        initLegalTenderDealList();
    }

    @Override
    public void onAnewRequest() {
        initLegalTenderDealList();
    }

    public static Fragment newInstance(int pageType, int uid) {
        Bundle bundle = new Bundle();
        bundle.putInt(C.EXTRA_PAGE_TYPE, pageType);
        bundle.putInt(C.EXTRA_UID, uid);
        UserLegalTenderDealListFragment fragment = new UserLegalTenderDealListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
}
